<template>
  <div class="trade-container">
    <h3 class="title">填写并核对订单信息</h3>
    <div class="content">
      <h5 class="receive">收件人地址</h5>
      <div class="address clearFix" ref="address" @click="checkAddress" id="1">
        <p v-for="(address, index) in addressList" :key="index">
          <span class="username" style="margin-top: 5px">{{ address }}</span>
          <a v-if="index != 0" @click="changeAddress(address)" id="1"
            >设为默认</a
          >
        </p>
      </div>

      <div class="line"></div>
      <h5 class="pay">支付方式</h5>
      <div class="address clearFix" ref="exchange" @click="checkExchange">
        <span class="username selected">在线支付</span>
        <span class="username" style="margin-left: 5px">线下支付</span>
      </div>
      <div class="line"></div>
      <h5 class="pay">送货清单</h5>
      <div class="detail">
        <h5>商品清单</h5>
        <ul
          class="list clearFix"
          v-for="wares in waresList"
          :key="wares.cartId"
        >
          <li>
            <p>
              {{ wares.wares.waresName }}
            </p>
          </li>
          <li>
            <img
              :src="wares.wares.pictureList[0]"
              style="width: 100px; height: 100px"
            />
          </li>
          <li>
            <h3>价格:{{ wares.wares.waresPrice }}元</h3>
          </li>
          <li>数量:{{ wares.waresNumber }}</li>
        </ul>
      </div>

      <div class="line"></div>
    </div>
    <div class="money clearFix">
      <ul>
        <li>
          <b
            ><i>{{ waresList.length }}</i
            >件商品，总商品金额</b
          >
          <span>{{ aggregateAmount }}元</span>
        </li>
      </ul>
    </div>
    <div class="trade">
      <div class="price">
        应付金额:　<span>{{ aggregateAmount }}元</span>
      </div>
      <div class="receiveInfo">
        寄送至:
        <span>{{ address }}</span>
        收货人：<span>{{ buyerInfo.buyerName }}</span>
        <span>{{ buyerInfo.buyerPhone }}</span>
      </div>
    </div>
    <div class="sub clearFix">
      <!--<router-link class="subBtn" to="/pay">提交订单</router-link>-->
      <a class="subBtn" @click="OrderToPay">提交订单</a>
    </div>
  </div>
</template>
  
  <script>
import { mapState, mapGetters } from "vuex";
export default {
  name: "Trade",
  data() {
    return {
      exchange: "在线支付",
      address: "",
    };
  },
  computed: {
    ...mapGetters(["cartList"]),
    ...mapState({
      buyerInfo: (state) => state.user.buyerInfo,
    }),
    addressList() {
      let address = this.buyerInfo.buyerAddress;
      return address.split(";");
    },
    waresList() {
      let newCartList = [];
      for (let i = 0; i < this.cartList.length; i++) {
        if (this.cartList[i].cartState == 1) {
          newCartList.push(this.cartList[i]);
        }
      }
      return newCartList;
    },
    aggregateAmount() {
      let sum = 0;
      for (let i = 0; i < this.waresList.length; i++) {
        sum +=
          this.waresList[i].waresNumber * this.waresList[i].wares.waresPrice;
      }
      return sum;
    },
  },

  methods: {
    //提交订单
    async OrderToPay() {
      try {
        if(this.exchange == "在线支付"){
          let ordersId=await this.$store.dispatch("insertCartToOrder", {address:this.address,payment:2});
          this.$router.push({ name: "pay",params: {ordersId:ordersId} });
        }else{
          await this.$store.dispatch("insertCartToOrder", {address:this.address,payment:1});
          this.$router.push({ path: "/center" });
        }
      } catch (error) {
        alert(error.message);
      }
    },
    //修改默认地址
    async changeAddress(address) {
      try {
        await this.$store.dispatch("changeDefaultAddress", address);
        await this.$store.dispatch("getBuyerInfo");
        let children = this.$refs.address.children;
        for (let i = 0; i < children.length; i++) {
          children[i].children[0].setAttribute("class", "username");
        }
        this.$refs.address.children[0].children[0].setAttribute(
          "class",
          "username selected"
        );
        this.address = this.addressList[0];
      } catch (error) {
        alert(error.message);
      }
    },
    checkExchange(event) {
      let children = this.$refs.exchange.children;
      for (let i = 0; i < children.length; i++) {
        children[i].setAttribute("class", "username");
      }
      event.target.setAttribute("class", "username selected");
      this.exchange = event.target.innerText;
    },
    checkAddress(event) {
      if (!event.target.getAttribute("id")) {
        let children = this.$refs.address.children;
        for (let i = 0; i < children.length; i++) {
          children[i].children[0].setAttribute("class", "username");
        }
        if (event.target.getAttribute("class")) {
          event.target.setAttribute("class", "username selected");
          this.address = event.target.innerText;
        } else {
          event.target.children[0].setAttribute("class", "username selected");
          this.address = event.target.children[0].innerText;
        }
      }
    },
  },
  mounted() {
    //获取购物车商品信息
    this.$store.dispatch("getCartList");
    this.$refs.address.children[0].children[0].setAttribute(
      "class",
      "username selected"
    );
    this.address = this.addressList[0];
  },
};
</script>
  
  <style lang="less" scoped>
.trade-container {
  .title {
    width: 1200px;
    margin: 0 auto;
    font-size: 14px;
    line-height: 21px;
  }

  .content {
    width: 1200px;
    margin: 10px auto 0;
    border: 1px solid rgb(221, 221, 221);
    padding: 25px;
    box-sizing: border-box;

    .receive,
    .pay {
      line-height: 36px;
      margin: 18px 0;
    }

    .address {
      padding-left: 20px;
      margin-bottom: 15px;

      .username {
        float: left;
        width: 100px;
        height: 25px;
        line-height: 25px;
        text-align: center;
        border: 1px solid #ddd;
        position: relative;
      }

      .username::after {
        content: "";
        display: none;
        width: 13px;
        height: 13px;
        position: absolute;
        right: 0;
        bottom: 0;
      }

      .username.selected {
        border-color: #e1251b;
      }

      .username.selected::after {
        display: block;
      }

      a {
        margin-left: 10px;
        width: 50px;
        height: 25px;
      }
      p {
        width: 610px;
        float: left;
        line-height: 30px;
        margin-left: 10px;
        padding-left: 5px;
        cursor: pointer;

        .s1 {
          float: left;
        }

        .s2 {
          float: left;
          margin: 0 5px;
        }

        .s3 {
          float: left;
          width: 56px;
          height: 24px;
          line-height: 24px;
          margin-left: 10px;
          background-color: #878787;
          color: #fff;
          margin-top: 3px;
          text-align: center;
        }
      }

      p:hover {
        background-color: #ddd;
      }
    }

    .line {
      height: 1px;
      background-color: #ddd;
    }

    .way {
      width: 1080px;
      height: 110px;
      background: #f4f4f4;
      padding: 15px;
      margin: 0 auto;

      h5 {
        line-height: 50px;
      }

      .info {
        margin-top: 20px;

        .s1 {
          float: left;
          border: 1px solid #ddd;
          width: 120px;
          height: 30px;
          line-height: 30px;
          text-align: center;
          margin-right: 10px;
        }
        .s1.selected {
          border-color: #e1251b;
        }

        p {
          line-height: 30px;
        }
      }
    }

    .detail {
      width: 1080px;

      background: #feedef;
      padding: 15px;
      margin: 2px auto 0;

      h5 {
        line-height: 50px;
      }

      .list {
        display: flex;
        justify-content: space-between;

        li {
          line-height: 30px;

          p {
            margin-bottom: 20px;
          }

          h4 {
            color: #c81623;
            font-weight: 400;
          }

          h3 {
            color: #e12228;
          }
        }
      }
    }

    .bbs {
      margin-bottom: 15px;

      h5 {
        line-height: 50px;
      }

      textarea {
        width: 100%;
        border-color: #e4e2e2;
        line-height: 1.8;
        outline: none;
        resize: none;
      }
    }

    .bill {
      h5 {
        line-height: 50px;
      }

      div {
        padding-left: 15px;
      }
    }
  }

  .money {
    width: 1200px;
    margin: 20px auto;

    ul {
      width: 220px;
      float: right;

      li {
        line-height: 30px;
        display: flex;
        justify-content: space-between;

        i {
          color: red;
        }
      }
    }
  }

  .trade {
    box-sizing: border-box;
    width: 1200px;
    padding: 10px;
    margin: 10px auto;
    text-align: right;
    background-color: #f4f4f4;
    border: 1px solid #ddd;

    div {
      line-height: 30px;
    }

    .price span {
      color: #e12228;
      font-weight: 700;
      font-size: 14px;
    }

    .receiveInfo {
      color: #999;
    }
  }

  .sub {
    width: 1200px;
    margin: 0 auto 10px;

    .subBtn {
      float: right;
      width: 164px;
      height: 56px;
      font: 700 18px "微软雅黑";
      line-height: 56px;
      text-align: center;
      color: #fff;
      background-color: #e1251b;
    }
  }
}
</style>